package com.javaweb.bookstore.component;

import com.javaweb.bookstore.entity.Book;
import com.javaweb.bookstore.entity.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;
import java.util.UUID;
@Service
public class FileRepository {
    static  final String file_path="book_images";
    public String storeFile(MultipartFile file,String id) throws IOException {

        InputStream in = file.getInputStream();
        byte[] buffer = new byte[1024];
        String fname = file.getOriginalFilename();
        String ext = fname.split("\\.")[1];
        File dir = new File(file_path);
        if(!dir.exists()){
            dir.mkdir();
        }

        File localFile = new File(file_path + "/" + id + "." + ext);

        OutputStream out = new FileOutputStream(localFile);



        while(true){
            int l = in.read(buffer);
            if(l <=0)
                break;
            out.write(buffer,0, l);

        }
        out.close();
        return fname;

    }
    public void stream(OutputStream out,String fname){
        new FileInputStream(file_path + "/")

    }

}
